﻿using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;

namespace Client {
    ///   <summary>
    ///   这个类可以让你得到一个在运行中程序的所有鼠标事件
    ///   并且引发一个带MouseEventArgs参数的.NET鼠标事件以便你很容易使用这些信息
    ///   </summary>
    public class CNxMouseHook {
        private const int WM_MOUSEMOVE = 0x200;
        private const int WM_LBUTTONDOWN = 0x201;
        private const int WM_RBUTTONDOWN = 0x204;
        private const int WM_MBUTTONDOWN = 0x207;
        private const int WM_LBUTTONUP = 0x202;
        private const int WM_RBUTTONUP = 0x205;
        private const int WM_MBUTTONUP = 0x208;
        private const int WM_LBUTTONDBLCLK = 0x203;
        private const int WM_RBUTTONDBLCLK = 0x206;
        private const int WM_MBUTTONDBLCLK = 0x209;
        //全局的事件
        public event MouseEventHandler MouseEvent;
        static int m_hMouseHook = 0;   //鼠标钩子句柄
        //鼠标常量
        public const int WH_MOUSE_LL = 14;   //mouse   hook   constant
        HookProc m_MouseHookProcedure;   //声明鼠标钩子事件类型.
        ////声明一个Point的封送类型
        [StructLayout(LayoutKind.Sequential)]
        public class CNxPOINT {
            public int x;
            public int y;
        }
        //声明鼠标钩子的封送结构类型
        [StructLayout(LayoutKind.Sequential)]
        public class CNxMouseHookStruct {
            public CNxPOINT pt;
            public int hWnd;
            public int wHitTestCode;
            public int dwExtraInfo;
        }
        //装置钩子的函数
        [DllImport("user32.dll ", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
        //卸下钩子的函数
        [DllImport("user32.dll ", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);

        //下一个钩挂的函数
        [DllImport("user32.dll ", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
        public delegate int HookProc(int nCode, Int32 wParam, IntPtr lParam);
        [DllImport("kernel32.dll")]
        private static extern IntPtr GetModuleHandle(IntPtr path);
        
        public CNxMouseHook() {
            Application.Current.Exit += new ExitEventHandler((object sender, ExitEventArgs e) => {
                Stop();
            });
        }
        public void Start() {
            //安装鼠标钩子
            if (m_hMouseHook == 0) {
                //生成一个HookProc的实例.
                m_MouseHookProcedure = new HookProc(MouseHookProc);
                m_hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, m_MouseHookProcedure, GetModuleHandle(IntPtr.Zero), 0);
                //如果装置失败停止钩子
                if (m_hMouseHook == 0) {
                    Console.WriteLine("如果装置失败停止钩子");
                    Stop();
                    throw new Exception("SetWindowsHookEx failed. ");
                }
            }
        }
        public void Stop() {
            Console.WriteLine("停止了");
            bool retMouse = true;
            if (m_hMouseHook != 0) {
                retMouse = UnhookWindowsHookEx(m_hMouseHook);
                m_hMouseHook = 0;
            }
        }
        private int MouseHookProc(int in_nCode, Int32 in_wParam, IntPtr in_lParam) {
            //从回调函数中得到鼠标的信息
            if (WM_LBUTTONUP == in_wParam) {
                CNxMouseHookStruct MyMouseHookStruct = (CNxMouseHookStruct)Marshal.PtrToStructure(in_lParam, typeof(CNxMouseHookStruct));
                MouseEventArgs m_MouseArgs = new MouseEventArgs(Mouse.PrimaryDevice, 0);
                MouseEvent(this, m_MouseArgs);
            }
            return CallNextHookEx(m_hMouseHook, in_nCode, in_wParam, in_lParam);
        }
    }
}